package com.m8n6.demo2.controller.admin;

import com.m8n6.demo2.common.http.ResponseResult;
import com.m8n6.demo2.common.security.anno.AuthIgnore;
import com.m8n6.demo2.common.security.utils.MyUtil;
import com.m8n6.demo2.entity.GoodsEntity;
import com.m8n6.demo2.service.admin.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/admin/goods/")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    private static byte[] filecontent;
    private static String fileName;
    @AuthIgnore //TODO
    @PostMapping("/getGoods")
    public ResponseResult<Map<String,Object>> getGoods(@RequestBody GoodsEntity goodsEntity){
        return goodsService.getGoods(goodsEntity);
    }
    @AuthIgnore//TODO
    @PostMapping("/getAdvGoods")
    public ResponseResult<List<GoodsEntity>> getAdvGoods(){
        return goodsService.getAdvGoods();
    }

    @AuthIgnore//TODO
    @PostMapping("/getGoodsIndex")
    public ResponseResult<List<GoodsEntity>> getGoodsIndex(@RequestBody GoodsEntity goodsEntity){
        return goodsService.getGoodsIndex(goodsEntity);
    }

    @AuthIgnore//TODO
    @PostMapping("/fileInit")
    public  void fileInit(@RequestBody MultipartFile file){
        try {
            filecontent = file.getBytes();
        } catch (IOException e) {
            throw new RuntimeException(e);
            }
        fileName = file.getOriginalFilename();
        }
    @AuthIgnore//TODO
    @PostMapping("/add")
    public ResponseResult<Map<String, String>> addGoods(@RequestBody GoodsEntity goodsEntity) {
        goodsEntity.setFileName(fileName);
        goodsEntity.setLogoFile(filecontent);
        return goodsService.addGoods(goodsEntity);
    }
    @AuthIgnore//TODO
    @PostMapping("/delete")
    public ResponseResult<Map<String, String>> deleteGoods(@RequestBody GoodsEntity goodsEntity) {

        return goodsService.deleteGoods(goodsEntity);
    }

}
